package com.zhl.common.service;

import com.zhl.common.configuration.EmailServiceProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @description 邮件发送服务
 * @Author: z156940l@outlook.com
 * @className EmailService
 * @Date: 2024/7/20 23:05
 */
@Slf4j
@AllArgsConstructor
@Component
public class EmailService {
    private final JavaMailSender javaMailSender;
    private final EmailServiceProperties emailServiceProperties;


    /**
     * 发送普通文本邮件
     *
     * @param to
     * @param subject
     * @param text
     */
    public void sendSimpleEmail(String to, String subject, String text) {
        // 定制纯文本邮件信息SimplemaileMassage
        SimpleMailMessage message = new SimpleMailMessage();
        //向指定的邮件发送信息
        message.setFrom(emailServiceProperties.getUsername());
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        try {
            //发送邮件
            javaMailSender.send(message);
            log.info("纯文本邮件发送成功");
        } catch (MailException e) {
            log.error("纯文本邮件发送失败:{}", e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 发送复杂附件邮件
     *
     * @param to
     * @param subject
     * @param text
     * @param filePath
     * @param rscId
     * @param rscPath
     */
    public void sendComplexEmail(String to, String subject, String text, String filePath, String rscId, String rscPath) {
        // 定制复杂邮件信息M
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            // 使用MimeMailMessageHelper帮助类，并设置multipart多部件使用为true
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(emailServiceProperties.getUsername());
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true);
            //设置邮件静态资源
            FileSystemResource res = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId, res);
            // 设置邮件附件
            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);
            // 发送邮件
            javaMailSender.send(message);
            log.info("复杂邮件发送成功");
        } catch (MessagingException e) {
            log.error("复杂邮件发送失败:{}", e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 发送模板邮件
     *
     * @param to
     * @param subject
     * @param content
     */
    public void sendTemplateEmail(String to, String subject, String content) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            // 使用MimeMessageHelper帮助类，并设置multipart多部件使用为true
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(emailServiceProperties.getUsername());
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            // 发送邮件
            javaMailSender.send(message);
            log.info("模板邮件发送成功");
        } catch (MessagingException e) {
            log.error("模板邮件发送失败:{}", e.getMessage());
            e.printStackTrace();
        }
    }
}
